﻿using Biz.Buffer;
using Biz.EF.Platform.SG;
using Hjt212Parser;
using log4net;
using System;
using System.Linq;

namespace Biz.Process
{
    public static class ProcessorData
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorData));
        /// <summary>
        /// 2011指令处理
        /// 1、处理实时数据
        /// 2、处理分钟数据（无？）
        /// 3、处理小时数据
        /// 4、处理日数据
        /// 5、处理月数据
        /// 6、处理年数据
        /// 7、处理实时监控
        /// </summary>
        /// <param name="content"></param>
        public static void Process2011(string content)
        {
            ProcessRealData(content);
            var hour = ProcessHourData(content);
            var day = ProcessDayData(content);
            var month = ProcessMonthData(content);
            var year = ProcessYearData(content);
            ProcessRealMonitor(content, hour, day, month, year);
        }
        private static void ProcessRealData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return;
                }

                try
                {
                    var master = new 主实时数据
                    {
                        企业基本信息ID = letport.企业基本信息ID,
                        企业排口ID = letport.ID,
                        监测时间 = datatime.Value,
                        RTD = Parser.GetDoubleByKey("B01-Rtd", content) ?? 0,
                        累计量 =
                     Parser.GetDecimalByKey("B01-Total", content) ?? 0,
                        污染物类别ID =
                            CommonInformation.GetContamType("B01") == null
                                ? 2
                                : CommonInformation.GetContamType("B01").ID
                    };
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("B01"))
                        {
                            var detail = new 从实时数据
                            {
                                RTD = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0,
                                污染物编码ID =
                                    CommonInformation.GetContamCode(code) == null
                                        ? 2
                                        : CommonInformation.GetContamCode(code).ID
                            };
                            master.从实时数据.Add(detail);
                        }
                    }
                    pCtx.主实时数据.Add(master);
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessMinuteData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return;
                }

                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day,
                        datatime.Value.Hour, datatime.Value.Minute / 10 * 10,
                        0);
                    var end = begin.AddMinutes(10);
                    var master = pCtx.主分钟数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new 主分钟数据
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            监测时间 = datatime.Value,
                            时段量 = Parser.GetDoubleByKey("B01-Cou", content) ?? 0,
                            污染物类别ID =
                                CommonInformation.GetContamType("B01") == null
                                    ? 2
                                    : CommonInformation.GetContamType("B01").ID
                        };
                        pCtx.主分钟数据.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = Parser.GetDoubleByKey("B01-Cou", content) ?? 0;

                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detail = master.从分钟数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail == null)
                            {
                                detail = new 从分钟数据
                                {
                                    Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0,
                                    污染物编码ID =
                                        CommonInformation.GetContamCode(code) == null
                                            ? 2
                                            : CommonInformation.GetContamCode(code).ID,
                                    Avg = Parser.GetDoubleByKey(code + "-Avg", content)
                                };
                                master.从分钟数据.Add(detail);
                            }
                            detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        private static double? ProcessHourData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return null;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return null;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return null;
                }

                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day,
                        datatime.Value.Hour, 0, 0);
                    var end = begin.AddHours(1);
                    var master = pCtx.主小时数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    double hourCou = 0;

                    var b01Cou
                        = Parser.GetDoubleByKey("B01-Cou", content);
                    if (b01Cou == null)
                    {
                        var recordBegin = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderBy(t => t.监测时间).FirstOrDefault();
                        var recordEnd = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderByDescending(t => t.监测时间).FirstOrDefault();
                        if (recordEnd != null && recordBegin != null)
                        {
                            hourCou = (double)(recordEnd.累计量 - recordBegin.累计量);
                        }
                    }
                    if (master == null)
                    {
                        master = new 主小时数据
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            监测时间 = datatime.Value,
                            时段量 = b01Cou ?? hourCou,
                            污染物类别ID =
                                CommonInformation.GetContamType("B01") == null
                                    ? 2
                                    : CommonInformation.GetContamType("B01").ID
                        };
                        pCtx.主小时数据.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = b01Cou ?? hourCou;
                    //todo:因子没有从实时数据表取平均等
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detail = master.从小时数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail == null)
                            {
                                detail = new 从小时数据
                                {
                                    Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0,
                                    污染物编码ID =
                                        CommonInformation.GetContamCode(code) == null
                                            ? 2
                                            : CommonInformation.GetContamCode(code).ID,
                                    Avg = Parser.GetDoubleByKey(code + "-Avg", content)
                                };
                                master.从小时数据.Add(detail);
                            }
                            detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                        }
                    }
                    pCtx.SaveChanges();
                    return master.时段量;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                return null;
            }
        }

        private static double? ProcessDayData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return null;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return null;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return null;
                }

                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day,
                        0, 0, 0);
                    var end = begin.AddDays(1);
                    var master = pCtx.主日数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    double hourCou = 0;

                    var b01Cou
                        = Parser.GetDoubleByKey("B01-Cou", content);
                    if (b01Cou == null)
                    {
                        var recordBegin = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderBy(t => t.监测时间).FirstOrDefault();
                        var recordEnd = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderByDescending(t => t.监测时间).FirstOrDefault();
                        if (recordEnd != null && recordBegin != null)
                        {
                            hourCou = (double)(recordEnd.累计量 - recordBegin.累计量);
                        }
                    }
                    if (master == null)
                    {
                        master = new 主日数据
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            监测时间 = datatime.Value,
                            时段量 = b01Cou ?? hourCou,
                            污染物类别ID =
                                CommonInformation.GetContamType("B01") == null
                                    ? 2
                                    : CommonInformation.GetContamType("B01").ID
                        };
                        pCtx.主日数据.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = b01Cou ?? hourCou;
                    //todo:因子没有从实时数据表取平均等
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detail = master.从日数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail == null)
                            {
                                detail = new 从日数据
                                {
                                    Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0,
                                    污染物编码ID =
                                        CommonInformation.GetContamCode(code) == null
                                            ? 2
                                            : CommonInformation.GetContamCode(code).ID,
                                    Avg = Parser.GetDoubleByKey(code + "-Avg", content)
                                };
                                master.从日数据.Add(detail);
                            }
                            detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                        }
                    }
                    pCtx.SaveChanges();
                    return master.时段量;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                return null;
            }
        }
        private static double? ProcessMonthData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return null;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return null;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return null;
                }

                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day,
                        0, 0, 0);
                    var end = begin.AddDays(1);
                    var master = pCtx.主月数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    double hourCou = 0;

                    var b01Cou
                        = Parser.GetDoubleByKey("B01-Cou", content);
                    if (b01Cou == null)
                    {
                        var recordBegin = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderBy(t => t.监测时间).FirstOrDefault();
                        var recordEnd = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderByDescending(t => t.监测时间).FirstOrDefault();
                        if (recordEnd != null && recordBegin != null)
                        {
                            hourCou = (double)(recordEnd.累计量 - recordBegin.累计量);
                        }
                    }
                    if (master == null)
                    {
                        master = new 主月数据
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            监测时间 = datatime.Value,
                            时段量 = b01Cou ?? hourCou,
                            污染物类别ID =
                                CommonInformation.GetContamType("B01") == null
                                    ? 2
                                    : CommonInformation.GetContamType("B01").ID
                        };
                        pCtx.主月数据.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = b01Cou ?? hourCou;
                    //todo:因子没有从实时数据表取平均等
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detail = master.从月数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail == null)
                            {
                                detail = new 从月数据
                                {
                                    Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0,
                                    污染物编码ID =
                                        CommonInformation.GetContamCode(code) == null
                                            ? 2
                                            : CommonInformation.GetContamCode(code).ID,
                                    Avg = Parser.GetDoubleByKey(code + "-Avg", content)
                                };
                                master.从月数据.Add(detail);
                            }
                            detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                        }
                    }
                    pCtx.SaveChanges();
                    return master.时段量;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                return null;
            }
        }

        private static double? ProcessYearData(string content)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return null;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return null;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return null;
                }

                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day,
                        0, 0, 0);
                    var end = begin.AddDays(1);
                    var master = pCtx.主年数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    double hourCou = 0;

                    var b01Cou
                        = Parser.GetDoubleByKey("B01-Cou", content);
                    if (b01Cou == null)
                    {
                        var recordBegin = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderBy(t => t.监测时间).FirstOrDefault();
                        var recordEnd = pCtx.主实时数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID).Where(t => t.企业排口ID == letport.ID)
                            .Where(t => t.监测时间 >= begin)
                            .Where(t => t.监测时间 < end).OrderByDescending(t => t.监测时间).FirstOrDefault();
                        if (recordEnd != null && recordBegin != null)
                        {
                            hourCou = (double)(recordEnd.累计量 - recordBegin.累计量);
                        }
                    }
                    if (master == null)
                    {
                        master = new 主年数据
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            监测时间 = datatime.Value,
                            时段量 = b01Cou ?? hourCou,
                            污染物类别ID =
                                CommonInformation.GetContamType("B01") == null
                                    ? 2
                                    : CommonInformation.GetContamType("B01").ID
                        };
                        pCtx.主年数据.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = b01Cou ?? hourCou;
                    //todo:因子没有从实时数据表取平均等
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detail = master.从年数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail == null)
                            {
                                detail = new 从年数据
                                {
                                    Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0,
                                    污染物编码ID =
                                        CommonInformation.GetContamCode(code) == null
                                            ? 2
                                            : CommonInformation.GetContamCode(code).ID,
                                    Avg = Parser.GetDoubleByKey(code + "-Avg", content)
                                };
                                master.从年数据.Add(detail);
                            }
                            detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                        }
                    }
                    pCtx.SaveChanges();
                    return master.时段量;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                return null;
            }
        }
        //private static void ProcessDayData(string content)
        //{
        //    using (var pCtx = new PlatformXpfContext())
        //    {
        //        var mn = Parser.GetStringByKey("MN", content);
        //        if (string.IsNullOrEmpty(mn))
        //        {
        //            return;
        //        }

        //        var datatime = Parser.GetDateTimeByKey("DataTime", content);
        //        if (!datatime.HasValue)
        //        {
        //            return;
        //        }

        //        var letport = CommonInformation.GetLetportByMn(mn);
        //        if (letport == null)
        //        {
        //            return;
        //        }

        //        try
        //        {
        //            var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day, 0, 0, 0);
        //            var end = begin.AddDays(1);
        //            var master = pCtx.主日数据
        //                .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
        //                .Where(t => t.企业排口ID == letport.ID)
        //                .Where(t => t.监测时间 >= begin)
        //                .FirstOrDefault(t => t.监测时间 < end);

        //            if (master == null)
        //            {
        //                master = new 主日数据
        //                {
        //                    企业基本信息ID = letport.企业基本信息ID,
        //                    企业排口ID = letport.ID,
        //                    监测时间 = datatime.Value,
        //                    时段量 = Parser.GetDoubleByKey("B01-Day", content) ?? 0,
        //                    污染物类别ID =
        //                        CommonInformation.GetContamType("B01") == null
        //                            ? 2
        //                            : CommonInformation.GetContamType("B01").ID
        //                };
        //                pCtx.主日数据.Add(master);
        //            }
        //            master.监测时间 = datatime.Value;
        //            master.时段量 = Parser.GetDoubleByKey("B01-Day", content) ?? 0;

        //            var codes = Parser.GetPollutantCodes(content);
        //            foreach (var code in codes)
        //            {
        //                var contamCodeId = CommonInformation.GetContamCode(code) == null
        //                    ? 2
        //                    : CommonInformation.GetContamCode(code).ID;
        //                if (!code.Contains("B01"))
        //                {
        //                    var detail = master.从日数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
        //                    if (detail == null)
        //                    {
        //                        detail = new 从日数据
        //                        {
        //                            Cou = Parser.GetDoubleByKey(code + "-Day", content) ?? 0,
        //                            污染物编码ID =
        //                                CommonInformation.GetContamCode(code) == null
        //                                    ? 2
        //                                    : CommonInformation.GetContamCode(code).ID,
        //                            Avg = Parser.GetDoubleByKey(code + "-Avg", content)
        //                        };
        //                        master.从日数据.Add(detail);
        //                    }
        //                    detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
        //                    detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
        //                }
        //            }
        //            pCtx.SaveChanges();
        //        }
        //        catch (Exception ex)
        //        {
        //            Log.Error(ex);
        //        }
        //    }
        //}
        //private static void ProcessMonthData(string content)
        //{
        //    using (var pCtx = new PlatformXpfContext())
        //    {
        //        var mn = Parser.GetStringByKey("MN", content);
        //        if (string.IsNullOrEmpty(mn))
        //        {
        //            return;
        //        }

        //        var datatime = Parser.GetDateTimeByKey("DataTime", content);
        //        if (!datatime.HasValue)
        //        {
        //            return;
        //        }

        //        var letport = CommonInformation.GetLetportByMn(mn);
        //        if (letport == null)
        //        {
        //            return;
        //        }

        //        try
        //        {
        //            var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, 1, 0, 0, 0);
        //            var end = begin.AddMonths(1);
        //            var master = pCtx.主月数据
        //                .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
        //                .Where(t => t.企业排口ID == letport.ID)
        //                .Where(t => t.监测时间 >= begin)
        //                .FirstOrDefault(t => t.监测时间 < end);

        //            if (master == null)
        //            {
        //                master = new 主月数据
        //                {
        //                    企业基本信息ID = letport.企业基本信息ID,
        //                    企业排口ID = letport.ID,
        //                    监测时间 = datatime.Value,
        //                    时段量 = Parser.GetDoubleByKey("B01-Mon", content) ?? 0,
        //                    污染物类别ID =
        //                        CommonInformation.GetContamType("B01") == null
        //                            ? 2
        //                            : CommonInformation.GetContamType("B01").ID
        //                };
        //                pCtx.主月数据.Add(master);
        //            }
        //            master.监测时间 = datatime.Value;
        //            master.时段量 = Parser.GetDoubleByKey("B01-Mon", content) ?? 0;

        //            var codes = Parser.GetPollutantCodes(content);
        //            foreach (var code in codes)
        //            {
        //                var contamCodeId = CommonInformation.GetContamCode(code) == null
        //                    ? 2
        //                    : CommonInformation.GetContamCode(code).ID;
        //                if (!code.Contains("B01"))
        //                {
        //                    var detail = master.从月数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
        //                    if (detail == null)
        //                    {
        //                        detail = new 从月数据
        //                        {
        //                            Cou = Parser.GetDoubleByKey(code + "-Mon", content) ?? 0,
        //                            污染物编码ID =
        //                                CommonInformation.GetContamCode(code) == null
        //                                    ? 2
        //                                    : CommonInformation.GetContamCode(code).ID,
        //                            Avg = Parser.GetDoubleByKey(code + "-Avg", content)
        //                        };
        //                        master.从月数据.Add(detail);
        //                    }
        //                    detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
        //                    detail.Cou = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
        //                }
        //            }
        //            pCtx.SaveChanges();
        //        }
        //        catch (Exception ex)
        //        {
        //            Log.Error(ex);
        //        }
        //    }
        //}
        //private static void ProcessYearData(string content)
        //{
        //    using (var pCtx = new PlatformXpfContext())
        //    {
        //        var mn = Parser.GetStringByKey("MN", content);
        //        if (string.IsNullOrEmpty(mn))
        //        {
        //            return;
        //        }

        //        var datatime = Parser.GetDateTimeByKey("DataTime", content);
        //        if (!datatime.HasValue)
        //        {
        //            return;
        //        }

        //        var letport = CommonInformation.GetLetportByMn(mn);
        //        if (letport == null)
        //        {
        //            return;
        //        }

        //        try
        //        {
        //            var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
        //            var end = begin.AddYears(1);

        //            var months = pCtx.主月数据.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
        //                .Where(t => t.企业排口ID == letport.ID)
        //                .Where(t => t.监测时间 >= begin).ToList();

        //            var master = pCtx.主年数据
        //                .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
        //                .Where(t => t.企业排口ID == letport.ID)
        //                .Where(t => t.监测时间 >= begin)
        //                .FirstOrDefault(t => t.监测时间 < end);

        //            if (master == null)
        //            {
        //                master = new 主年数据
        //                {
        //                    企业基本信息ID = letport.企业基本信息ID,
        //                    企业排口ID = letport.ID,
        //                    监测时间 = datatime.Value,
        //                    时段量 = Parser.GetDoubleByKey("B01-Mon", content) ?? 0,
        //                    污染物类别ID =
        //                        CommonInformation.GetContamType("B01") == null
        //                            ? 2
        //                            : CommonInformation.GetContamType("B01").ID
        //                };
        //                pCtx.主年数据.Add(master);
        //            }
        //            else
        //            {
        //                master.监测时间 = datatime.Value;
        //                master.时段量 = months.Sum(t => t.时段量) ?? 0;
        //            }

        //            var codes = Parser.GetPollutantCodes(content);
        //            foreach (var code in codes)
        //            {
        //                var contamCodeId = CommonInformation.GetContamCode(code) == null
        //                    ? 2
        //                    : CommonInformation.GetContamCode(code).ID;
        //                if (!code.Contains("B01"))
        //                {
        //                    var detail = master.从年数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
        //                    if (detail == null)
        //                    {
        //                        detail = new 从年数据
        //                        {
        //                            Cou = Parser.GetDoubleByKey(code + "-Mon", content) ?? 0,
        //                            污染物编码ID =
        //                                CommonInformation.GetContamCode(code) == null
        //                                    ? 2
        //                                    : CommonInformation.GetContamCode(code).ID,
        //                            Avg = Parser.GetDoubleByKey(code + "-Avg", content)
        //                        };
        //                        master.从年数据.Add(detail);
        //                    }
        //                    else
        //                    {
        //                        detail.Cou = 0;
        //                        foreach (var month in months)
        //                        {
        //                            var item = month.从月数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
        //                            if (item != null)
        //                            {
        //                                detail.Cou += item.Cou;
        //                            }
        //                        }
        //                        detail.Avg = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;

        //                    }

        //                }
        //            }
        //            pCtx.SaveChanges();
        //        }
        //        catch (Exception ex)
        //        {
        //            Log.Error(ex);
        //        }
        //    }
        //}
        private static void ProcessRealMonitor(string content, double? hour, double? day, double? month, double? year)
        {
            using (var pCtx = new PlatformXpfContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn))
                {
                    return;
                }

                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue)
                {
                    return;
                }

                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null)
                {
                    return;
                }

                try
                {
                    var masterMonitor = pCtx.主实时监控
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.企业排口ID == letport.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new 主实时监控
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            企业排口ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.主实时监控.Add(masterMonitor);
                    }

                    masterMonitor.实时数据时间 = datatime;
                    masterMonitor.分钟数据时间 = datatime;
                    masterMonitor.瞬时量 = Parser.GetDoubleByKey("B01-Rtd", content) ?? 0;
                    masterMonitor.累计量 = Parser.GetDecimalByKey("B01-Cou", content);
                    if (!masterMonitor.累计量.HasValue)
                    {
                        masterMonitor.累计量 = Parser.GetDecimalByKey("B01-Total", content) ?? 0;
                    }
                    masterMonitor.日排放量 = Parser.GetDoubleByKey("B01-Day", content) ?? day ?? 0;
                    masterMonitor.月排放量 = Parser.GetDoubleByKey("B01-Mon", content) ?? month ?? 0;

                    Log.Info(masterMonitor.实时数据时间 + "->" + masterMonitor.瞬时量 + "->" + masterMonitor.小时排放量);
                    var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
                    var end = begin.AddYears(1);
                    var master = pCtx.主年数据
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.企业排口ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);
                    if (master != null)
                    {
                        masterMonitor.年排放量 = master.时段量;
                    }
                    var codes = Parser.GetPollutantCodes(content);
                    foreach (var code in codes)
                    {
                        Log.Info("code->" + code);
                        var contamCodeId = CommonInformation.GetContamCode(code) == null
                            ? 2
                            : CommonInformation.GetContamCode(code).ID;
                        if (!code.Contains("B01"))
                        {
                            var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detailMonitor == null)
                            {
                                detailMonitor = new 从实时监控
                                {
                                    污染物编码ID = CommonInformation.GetContamCode(code).ID
                                };
                                masterMonitor.从实时监控.Add(detailMonitor);
                            }

                            detailMonitor.瞬时量 = Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0;
                            //detailMonitor.小时排放量 = Parser.GetDoubleByKey(code + "-Cou", content) ?? 0;
                            detailMonitor.日排放量 = Parser.GetDoubleByKey(code + "-Day", content) ?? 0;
                            detailMonitor.月排放量 = Parser.GetDoubleByKey(code + "-Mon", content) ?? 0;
                            Log.Info(code + "-Rtd" + "->" + (Parser.GetDoubleByKey(code + "-Rtd", content) ?? 0));
                            Log.Info("->" + detailMonitor.瞬时量 + "->" + detailMonitor.小时排放量);
                            var detail = master?.从年数据.FirstOrDefault(t => t.污染物编码ID == contamCodeId);
                            if (detail != null)
                            {
                                detailMonitor.年排放量 = detail.Cou;
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
    }
}
